package GTGExpession;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 
 */

/**
 * Class for keeping expression as a list of OR statements
 * ENG: xor operand contains sum elements
 * PL:  operandami funkcji xor sa skaadniki sumy
 * @author Tomasz Werszko
 * @author Rafal Magda
 */

public class GTGXor {
	public ArrayList<GTGOr> ors = new ArrayList<GTGOr>();
	
	public GTGXor(String text){
		ors = new ArrayList<GTGOr>();
		
		int orBegin=0, orEnd=0;
		boolean end=false;
		while(end==false){
			orEnd = text.indexOf("^",orBegin);
			if(orEnd<0){
				orEnd=text.length();
				end=true;
			}
			String s_or = text.substring(orBegin, orEnd);
			ors.add(new GTGOr(s_or));
			orBegin=orEnd+1;
		}
	}

	public GTGXor() {
		ors = new ArrayList<GTGOr>();
	}
	
	/**
	 * Returns readable form of GTGXor expression
	 * @return readable form of boolean expression
	 */
	public String toString(){
		// TODO brackets ("()") are not tested 
		String result = new String();
		
		for(Iterator<GTGOr> iter = ors.iterator(); iter.hasNext();  ){
			GTGOr tmp=iter.next();
			if(tmp.ands.size()>1 || tmp.ands.get(0).variables.size()>1){
				result+="(";
			}
			result+=tmp.toString();
			if(tmp.ands.size()>1 || tmp.ands.get(0).variables.size()>1){
				result+=")";
			}
			if(iter.hasNext()){
				result+=" ^ ";
			}
		}
		return result;
	}
}
